package com.example.blogdemo.controller;

import com.example.blogdemo.mapper.BlogMapper;
import com.example.blogdemo.pojo.dataobject.BlogInfo;
import com.example.blogdemo.pojo.request.AddRequest;
import com.example.blogdemo.pojo.request.UpdateBlogRequest;
import com.example.blogdemo.pojo.respond.BlogRespond;
import com.example.blogdemo.pojo.respond.BlogRespondOne;
import com.example.blogdemo.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogInfoController {


    @Resource(name = "blogServiceImpl")
    private BlogService blogService;
    @Autowired
    private BlogMapper blogMapper;

    @RequestMapping("/getList")
    public List<BlogRespond> getList() {
        log.info("获取博客列表...");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogRespondOne getBlogDetail(@NotNull(message = "参数不能为null") Integer blogId) {

        log.info("获取详情页...");
        return blogService.getBlog(blogId);

    }

    @RequestMapping("/add")
    public boolean addBlog(@RequestBody @Validated AddRequest addRequest) {

        log.info("title, content:{}", addRequest.getTitle(), addRequest.getContent());
        return blogService.addBlgo(addRequest);
    }

    @RequestMapping("/delete")
    public Boolean BlogDelete(@NotNull(message = "blogId 不能为空") Integer blogId) {

        log.info("删除博客，blogId:{}", blogId);
        return blogService.blogDelete(blogId);

    }

    @RequestMapping("/update")
    public Boolean BlogUpdate(@RequestBody @Validated UpdateBlogRequest updateBlogRequest) {

        log.info("更新文章，id:{}", updateBlogRequest.getId());
        return blogService.blogUpdata(updateBlogRequest);

    }
}
